<%
xml = Builder::XmlMarkup.new(:indent=>2)
xml.instruct!
xml.kml(:xmlns => "http://earth.google.com/kml/2.2") {
  xml.Document {


    # contributors layer
    xml.Folder{
      xml.name("districts")
      @districts.each {|r|
        area= r.surf
        density=(r.population.to_f/area.to_f)*1000
        puts "#{r.population} #{r.surf} #{density}"
        coord=""
                r.geom[0].each { |point|
                  coord+="#{point.lng},#{point.lat},#{density.to_f*20} \n"
                }
        xml.Placemark{
          xml.name("District #{r.tln}")
          xml.description("")
          color=get_color_loudness((rand()*80.0)+30)
          xml.Style{  
            xml.LineStyle{
              xml.color(color)
              xml.colorMode("normal")
              xml.width("1")
              xml.fill(true) #xml.outline(false)
            }
            xml.PolyStyle{
              xml.color(color)
              xml.colorMode("normal")
              xml.outline(1)
              xml.fill(true)
            }
          }
          # set the color according the loudness set the height according to the
          # intensity of the contribution

          xml.Polygon{
            xml.extrude(true)
            xml.tessellate(false)
            xml.altitudeMode("relativeToGround")
            xml.outerBoundaryIs{
              xml.LinearRing{
                xml.coordinates(coord)
              }
            }
            #xml<< "#{r.geom.as_kml}"
          }


        }
      }
    }
    #xml.Folder{
    #xml.name("Corrected")
    #@measures.each_with_index { |point, i|
    #xml.Placemark{
    #xml.name("corrected")      
    #xml.description("#{point.location}")
    #xml.Point{
    #xml.coordinates("#{point.corrected.lng},#{point.corrected.lat},#{point.loudness}")
    #}

    #}
    #}
    #}
  }

}


%><%= xml.target! %>